package yue.shen.android.demo.activity

import android.graphics.Rect
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener
import com.scwang.smartrefresh.layout.util.DensityUtil
import yue.shen.android.demo.databinding.ActivityDemo1Binding

class Demo1Activity : AppCompatActivity(), OnRefreshLoadMoreListener {

    private val adapter = Demo1Adapter(mutableListOf())

    private lateinit var viewBinding: ActivityDemo1Binding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewBinding = ActivityDemo1Binding.inflate(layoutInflater)
        setContentView(viewBinding.root)

        viewBinding.refreshLayout.setEnableRefresh(true)
        viewBinding.refreshLayout.setEnableLoadMore(true)
        viewBinding.refreshLayout.setOnRefreshLoadMoreListener(this)

        val manager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
        viewBinding.recycleView.layoutManager = manager

        val divider = DensityUtil.dp2px(20f)

        viewBinding.recycleView.addItemDecoration(object : RecyclerView.ItemDecoration() {
            override fun getItemOffsets(
                outRect: Rect,
                view: View,
                parent: RecyclerView,
                state: RecyclerView.State
            ) {
                if (adapter.getItemViewType(parent.getChildAdapterPosition(view)) == 0) {
                    val layoutParams = view.layoutParams as StaggeredGridLayoutManager.LayoutParams
                    val spanIndex = layoutParams.spanIndex
                    outRect.top = divider
                    if (spanIndex == 0) {
                        outRect.left = divider
                        outRect.right = divider / 2
                    } else {
                        outRect.right = divider
                        outRect.left = divider / 2
                    }
                }
            }
        })

        viewBinding.recycleView.adapter = adapter


        viewBinding.refreshLayout.autoRefresh()
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        val list = mutableListOf<Demo1Data>()
        for (i in 0 until 10) {
            list.add(Demo1Data(0, "$i"))
        }
        adapter.updateData(list)
        stopRefresh()
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        val list = adapter.getData()
        val size = list.size

        if (size != 50) {
            val newList = mutableListOf<Demo1Data>()
            for (i in size until size + 10) {
                newList.add(Demo1Data(0, "$i"))
            }
            adapter.addData(newList)
        } else {
            adapter.addFooter()
        }

        stopRefresh()
    }

    private fun stopRefresh() {
        viewBinding.refreshLayout.finishLoadMore()
        viewBinding.refreshLayout.finishRefresh()
    }

}